package com.goku.gobang;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 人人对战游戏界面
 * @author goku
 *
 */
public class PlayersActivity extends Activity {

    /**
     * 黑棋剩余次数提示控件
     */
    private TextView blackCountView;
    /**
     * 白棋剩余次数提示控件
     */
    private TextView whiteCountView;
    /**
     * 棋盘对象
     */
    private ChessboardView chessboardView;
    /**
     * 黑棋悔棋按钮
     */
    private Button blackButton;
    /**
     * 白棋悔棋按钮
     */
    private Button whiteButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player_layout);
        chessboardView = (ChessboardView) findViewById(R.id.chessView);
        blackButton = (Button) findViewById(R.id.bt_blackWithdraw);
        blackButton.setOnClickListener(new buttonListener());
        whiteButton = (Button) findViewById(R.id.bt_whiteWithdraw);
        whiteButton.setOnClickListener(new buttonListener());
        blackCountView = (TextView) findViewById(R.id.tv_blackCount);
        whiteCountView = (TextView) findViewById(R.id.tv_whiteCount);
        blackCountView.setText(getText(R.string.blackcount)
                + String.valueOf(chessboardView.getBlackCount()));
        whiteCountView.setText(getText(R.string.whitecount)
                + String.valueOf(chessboardView.getWhiteCount()));
    }

    /**
     * 按钮监听器
     * @author goku
     */
    class buttonListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            if (chessboardView.isIswithdraw()) {
                if (v.getId() == R.id.bt_blackWithdraw) {
                    if (chessboardView.getBlackCount() > 0) {
                        chessboardView.withDraw(
                                chessboardView.getBlackPoints(), chessboardView
                                        .getWhitePoints(),
                                !chessboardView.getWhichPress(
                                        chessboardView.getLastPointx(),
                                        chessboardView.getLastPointy()), true,
                                chessboardView.getBlackCount());
                        if (chessboardView.isHaswithdraw()) {
                            blackCountView.setText(getText(R.string.blackcount)
                                    + String.valueOf(chessboardView
                                            .getBlackCount()));
                            chessboardView.setHaswithdraw(false);
                        }
                    } else {
                        toast();
                    }
                } else if (v.getId() == R.id.bt_whiteWithdraw) {
                    if (chessboardView.getWhiteCount() > 0) {
                        chessboardView.withDraw(
                                chessboardView.getWhitePoints(), chessboardView
                                        .getBlackPoints(),
                                chessboardView.getWhichPress(
                                        chessboardView.getLastPointx(),
                                        chessboardView.getLastPointy()), false,
                                chessboardView.getWhiteCount());
                        if (chessboardView.isHaswithdraw()) {
                            whiteCountView.setText(getText(R.string.whitecount)
                                    + String.valueOf(chessboardView
                                            .getWhiteCount()));
                            chessboardView.setHaswithdraw(false);
                        }
                    } else {
                        toast();
                    }
                }
            }
        }
    }

    /**
     * 悔棋次数用完对话框
     */
    public void toast() {
        Toast.makeText(getApplicationContext(), "您悔棋机会已使用完", Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 1, 0, R.string.restart);
        menu.add(0, 2, 0, R.string.quit);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                chessboardView.assign();
                chessboardView.invalidate();
                blackCountView.setText(getText(R.string.blackcount)
                        + String.valueOf(chessboardView.getBlackCount()));
                whiteCountView.setText(getText(R.string.whitecount)
                        + String.valueOf(chessboardView.getWhiteCount()));
                break;
            case 2:
                finish();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
